using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace ChuyenDongVatLy
{
   
    public class ChuyenDongNemXien : Microsoft.Xna.Framework.GameComponent
    {
        private BaseObject _object;
        public Texture2D imageTinh;
        public Texture2D imageDong;

        private Texture2D picPlay;
        private Texture2D picStop;
        private Texture2D picPause;
        private Texture2D picControl;

        private Texture2D picPlay2;
        private Texture2D picStop2;
        private Texture2D picPause2;
        private Texture2D picControl2;
        private SpriteFont font1;

        private Texture2D picKhung;
        private Texture2D picKhung2;
        private Texture2D picBackGround;

        private bool _isPause = false;
        private bool _isPlay = false;
        private bool _isStop = false;
        private bool _isControl = false;

        private bool isPause = false;

        MouseGame mouse;    

        Game1 game;
        BaseObject _vatthe;
        float vantocbandau= 600.0f;
        double phabandau= Math.PI/6;
        Vector2 vitribandau = new Vector2(0, 300);
        Camera camera = new Camera();
        Texture2D thanh;

        Texture2D picArrow;
        Texture2D picArrowLeft;
        Texture2D picArrowUp;
        Texture2D picArrowDown;
        
        SpriteFont font;
        Texture2D picPosGone;
        Texture2D picMatDat;

        List<Vector2> _PosGone;
        int[] xThanh;
        int[] yThanh;
        string ho;
        float _vx;
        float _vy;
        float _thoiGian;
        double _gocLucTH;
        double _dolonVanTocTH;
        private bool isDrawFly = true;
        private float x;
        private float y;
        public ChuyenDongNemXien(Game1 game1)
            : base(game1)
        {
            mouse = new MouseGame(game1);
            this.game = game1;
            _vatthe = new BaseObject();
            _vatthe.Pos = vitribandau;
            _vatthe.AnBegin = phabandau;
            _vatthe.V0 = vantocbandau;
            xThanh= new int[100];
            yThanh = new int[100];
            _PosGone = new List<Vector2>();
            Timer TimerDraw = new Timer();
            TimerDraw.Enabled = true;
            TimerDraw.Interval = 100;
            TimerDraw.Tick += new EventHandler(TimerDraw_Tick);

        }

        void TimerDraw_Tick(object sender, EventArgs e)
        {
            if (isDrawFly)
                isDrawFly = false;
            else
                isDrawFly = true;
        }
         
        public ChuyenDongNemXien(Game1 game1,float vantocbandau1, double phabandau1)
            : base(game1)
        {
            this.game = game1;
            this.vantocbandau = vantocbandau1;
            this.phabandau = phabandau1;
            xThanh = new int[100];
            yThanh = new int[100];
        }
        public override void Update(GameTime gameTime)
        {
            CreateForm();
            MouseOverButton();
            CheckButton();
            ChuyenDong(gameTime);
            TinhLucTH();
            base.Update(gameTime);
        }
        public override void Initialize()
        {
          
            base.Initialize();
        }
        public void LoadContent(ContentManager Content)
        {
            picKhung = Content.Load<Texture2D>("DDDH\\khung");
            picKhung2 = Content.Load<Texture2D>("DDDH\\khung2");
            picBackGround = Content.Load<Texture2D>("NX\\backgroundNX");

            picControl = Content.Load<Texture2D>("Button\\b_Control");
            picPlay = Content.Load<Texture2D>("Button\\b_Play");
            picPause = Content.Load<Texture2D>("Button\\b_Pause");
            picStop = Content.Load<Texture2D>("Button\\b_Stop");


            picControl2 = Content.Load<Texture2D>("Button\\a_Control");
            picPlay2 = Content.Load<Texture2D>("Button\\a_Play");
            picPause2 = Content.Load<Texture2D>("Button\\a_Pause");
            picStop2 = Content.Load<Texture2D>("Button\\a_Stop");

            imageTinh = Content.Load<Texture2D>("NX\\tinh");
            imageDong = Content.Load<Texture2D>("NX\\dong");

           _vatthe.Picture= Content.Load<Texture2D>("ball");
           thanh = Content.Load<Texture2D>("Thanh");
           picArrow = Content.Load<Texture2D>("NX\\arrow");
           picArrowLeft = Content.Load<Texture2D>("NX\\arrowleft");
           picArrowUp = Content.Load<Texture2D>("NX\\arrowup");
           picArrowDown = Content.Load<Texture2D>("NX\\arrowdow");
           font = Content.Load<SpriteFont>("font");
           picPosGone = Content.Load<Texture2D>("bombfrag");
           picMatDat = Content.Load<Texture2D>("gach");

        }
        public void ChuyenDong(GameTime gameTime)
        {
            if (isPause== false)
            {
                if (_vatthe.Pos.Y >=ConstPhysic.DoCaoMatDat-60)
                {
                    isPause = true;
                }
                TimeGame._gameTime += ((float)gameTime.ElapsedGameTime.Milliseconds / 1000);
                x = (float)((_vatthe.V0 * Math.Cos(phabandau)) * TimeGame._gameTime);
                 y = -(float)((_vatthe.V0 * Math.Sin(phabandau)) * TimeGame._gameTime - 0.5 * ConstPhysic.Gravity * TimeGame._gameTime * TimeGame._gameTime);
                _vx = (float)(_vatthe.V0 * Math.Cos(phabandau));
                _vy = (float)(_vatthe.V0 * Math.Sin(phabandau)) - ConstPhysic.Gravity * TimeGame._gameTime;
                _vatthe.Pos = new Vector2(x, y) + vitribandau;
            }
            
        }
      
        public void Draw(SpriteBatch sp)
        {

            sp.Draw(picBackGround, new Rectangle(0, 0, 800, 700), Color.White);//bg
               
            Camera ca = new Camera();
            float xOb,yOb;
            xOb= _vatthe.Pos.X;
            yOb=_vatthe.Pos.Y;
            ca.UpdateCamera(xOb,yOb);
            float x,y;
            x = xOb - ca.xcamera;
            y = yOb - ca.ycamera;                                                                                                                                                     
           
            if (isDrawFly)
            {
                _PosGone.Add(new Vector2(_vatthe.Pos.X,_vatthe.Pos.Y));
            }
            for (int i = 0; i < _PosGone.Count; i++)
            {
                sp.Draw(picPosGone,new Rectangle((int)(_PosGone[i].X-ca.xcamera+30),(int)(_PosGone[i].Y-ca.ycamera+30),10,10),Color.White);
            }
            sp.Draw(_vatthe.Picture, new Rectangle((int)x, (int)y, 60, 60), Color.White);
            sp.Draw(picArrow, new Rectangle((int)x + 20+5, (int)y + 20, (int)_vx / 3, 10), new Rectangle(0, 0, 100, 10), Color.White, 0, Vector2.Zero, SpriteEffects.None, 0);
            if (_vy > 0)
            { 
                sp.Draw(picArrow, new Rectangle((int)x + 20+2, (int)y + 20+4, (int)_vy / 3, 10), new Rectangle(0, 0, 100, 10), Color.White, (float)-Math.PI / 2, Vector2.Zero, SpriteEffects.None, 0);
                sp.Draw(picArrow, new Rectangle((int)x + 20+5, (int)y + 20, (int)_dolonVanTocTH / 3, 10), new Rectangle(0, 0, 100, 10), Color.White, (float)-_gocLucTH, Vector2.Zero, SpriteEffects.None, 0);
            }
            else
            {
                sp.Draw(picArrow, new Rectangle((int)x + 20+12, (int)y + 20+5, (int)-_vy / 3, 10), new Rectangle(0, 0, 100, 10), Color.White, (float)Math.PI / 2, Vector2.Zero, SpriteEffects.None, 0);
                sp.Draw(picArrow, new Rectangle((int)x + 20+5, (int)y + 20, (int)_dolonVanTocTH / 3, 10), new Rectangle(0, 0, 100, 10), Color.White, (float)-_gocLucTH, Vector2.Zero, SpriteEffects.None, 0);
            }
            
            sp.Draw(picMatDat, new Rectangle(0, (int)(ConstPhysic.DoCaoMatDat-ca.ycamera), 10000, 10), Color.White);
            
            sp.Draw(picKhung, new Rectangle(0, 0, 900, 100), Color.White);//khung
            sp.Draw(picKhung2, new Rectangle(620, 100, 180, 700), Color.White);//khung2       
            sp.Draw(imageTinh, ConstPhysic.tinh, Color.White);
            sp.Draw(imageDong, ConstPhysic.dong, Color.White);
            sp.DrawString(font,_vatthe.V0.ToString() + "(m/s)",new Vector2(700,28),Color.Blue);
                sp.DrawString(font,Math.Round(this.phabandau/Math.PI,2,MidpointRounding.AwayFromZero).ToString() +"*pi", new Vector2(700, 78), Color.Blue);
           if(ho == null)
                sp.DrawString(font,(Math.Round(this.vitribandau.Y,2,MidpointRounding.AwayFromZero)).ToString() + "(m)", new Vector2(700, 128), Color.Blue);
           else
               sp.DrawString(font, this.ho+"(m)", new Vector2(700, 128), Color.Blue);

            sp.DrawString(font, Math.Round( _dolonVanTocTH,2,MidpointRounding.AwayFromZero).ToString() + "(m/s)", new Vector2(690, 328), Color.Blue);
            sp.DrawString(font, Math.Round((ConstPhysic.DoCaoMatDat-_vatthe.Pos.Y),2,MidpointRounding.AwayFromZero).ToString() + "(m)",new Vector2(690,475),Color.Blue);
            sp.DrawString(font, Math.Round((_vatthe.Pos.X), 2, MidpointRounding.AwayFromZero).ToString() + "(m)", new Vector2(690,378), Color.Blue);
            sp.DrawString(font,Math.Round(_gocLucTH/Math.PI,2,MidpointRounding.AwayFromZero)+"*pi",new Vector2(690,428),Color.Blue);
            if (_isPlay)
                sp.Draw(picPlay2, ConstPhysic.btnPlay2, Color.White);
            else
                sp.Draw(picPlay, ConstPhysic.btnPlay, Color.White);
            if (_isStop)
                sp.Draw(picStop2, ConstPhysic.btnStop2, Color.White);
            else
                sp.Draw(picStop, ConstPhysic.btnStop, Color.White);
            if (_isPause)
                sp.Draw(picPause2, ConstPhysic.btnPause2, Color.White);
            else
                sp.Draw(picPause, ConstPhysic.btnPause, Color.White);
            if (_isControl)
                sp.Draw(picControl2, ConstPhysic.btnControl2, Color.White);
            else
                sp.Draw(picControl, ConstPhysic.btnControl, Color.White);
        }

        public void DrawButton(SpriteBatch sp)
        {
            if (_isPlay)
                sp.Draw(picPlay2, ConstPhysic.btnPlay2, Color.White);
            else
                sp.Draw(picPlay, ConstPhysic.btnPlay, Color.White);
            if (_isStop)
                sp.Draw(picStop2, ConstPhysic.btnStop2, Color.White);
            else
                sp.Draw(picStop, ConstPhysic.btnStop, Color.White);
            if (_isPause)
                sp.Draw(picPause2, ConstPhysic.btnPause2, Color.White);
            else
                sp.Draw(picPause, ConstPhysic.btnPause, Color.White);
            if (_isControl)
                sp.Draw(picControl2, ConstPhysic.btnControl2, Color.White);
            else
                sp.Draw(picControl, ConstPhysic.btnControl, Color.White);
        }

        public void CheckButton()
        {
            if (mouse.CheckMouse(ConstPhysic.btnPlay) && Mouse.GetState().LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            {
                _isPlay = true;
                isPause = false;
            }
            if (mouse.CheckMouse(ConstPhysic.btnPause) && Mouse.GetState().LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            {
                isPause = true;
            }
            if (mouse.CheckMouse(ConstPhysic.btnStop) && Mouse.GetState().LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            {
                isPause = true;
                ReSet();
            }
        }
        private void ReSet()
        {
            _vatthe.Pos = vitribandau;
            TimeGame._gameTime = 0;
            _PosGone.Clear();
        }
        public void MouseOverButton()
        {
            if (mouse.CheckMouse(ConstPhysic.btnControl)) _isControl = true;
            else _isControl = false;
            if (mouse.CheckMouse(ConstPhysic.btnPause)) _isPause = true;
            else _isPause = false;
            if (mouse.CheckMouse(ConstPhysic.btnPlay)) _isPlay = true;
            else _isPlay = false;
            if (mouse.CheckMouse(ConstPhysic.btnStop)) _isStop = true;
            else _isStop = false;
        }
        public void TinhLucTH()
        {
            double tgGocTh = (double)_vy / _vx;
            _gocLucTH = Math.Atan(tgGocTh);
            _dolonVanTocTH = Math.Sqrt(_vx*_vx+_vy*_vy);
        }
        public void CreateForm()
        {
            if (mouse.CheckMouse(ConstPhysic.btnControl) && TimeGame._ShowForm && Mouse.GetState().LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            {
                frNemxien fr = new frNemxien();
                fr.GetMyValue = new frNemxien.GetString(SetValue);
                fr.GetMyValue2 = new frNemxien.GetString2(SetValue2);
                fr.Show();
                TimeGame._ShowForm = false;
            }
        }
        public void SetValue(string vantoc ,string gocnghieng,string docao)
        {
            float vantoc1= float.Parse(vantoc);
            float gocnghieng1= float.Parse(gocnghieng);
            float docao1= float.Parse(docao);
            this._vatthe.V0 = vantoc1;
            this.phabandau = (float)(gocnghieng1*Math.PI);
            this.vitribandau = new Vector2(0,ConstPhysic.DoCaoMatDat- docao1);
            this.ho = docao;
        }
        public void SetValue2(bool CreateForm)
        {
            TimeGame._ShowForm = CreateForm;
        }
       
    }
}
